package cn.com.chec.en.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.constants.LogConstants;
import cn.com.chec.en.dao.LogInfoDao;
import cn.com.chec.en.dao.UserInfoDao;
import cn.com.chec.en.model.domain.LogInfo;
import cn.com.chec.en.model.domain.UserInfo;
import cn.com.chec.en.model.dto.LogReportDTO;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.TimeUtils;

@Controller
@RequestMapping({ "/admin/log" })
public class LogAdminController {
	protected static final Logger logger = Logger
			.getLogger(LogAdminController.class);

	@Resource
	private UserInfoDao userInfoDao;

	@Resource
	private LogInfoDao logInfoDao;

	@Resource
	private Map<String, Object> environment;

	@RequestMapping({ "/list" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/log/logList");

		List<UserInfo> userInfoList = this.userInfoDao.getAllUserName();
		int logActionId = StringUtils.isNotBlank(request
				.getParameter("logActionId")) ? Integer.parseInt(request
				.getParameter("logActionId")) : 0;
		int userId = StringUtils.isNotBlank(request.getParameter("userId")) ? Integer
				.parseInt(request.getParameter("userId")) : 0;

		String startDate = StringUtils.isBlank(request
				.getParameter("startDate")) ? TimeUtils.getDate(new Date(), -6)
				: request.getParameter("startDate");
		String endDate = StringUtils.isBlank(request.getParameter("endDate")) ? TimeUtils
				.getDate(new Date(), 0) : request.getParameter("endDate");
		LogInfo query = new LogInfo();
		query.setLogActionId(logActionId);
		query.setUserId(userId);
		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<LogInfo> logList = this.logInfoDao.getSubList(query, startDate, endDate,
				pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.logInfoDao.getCount(query, startDate,
				endDate));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("actionMap", LogConstants.LOGACTIONMAP);
		map.put("userInfoList", userInfoList);
		map.put("logInfoList", logList);
		map.put("pageBean", pageBean);
		map.put("logActionId", Integer.valueOf(logActionId));
		map.put("userId", Integer.valueOf(userId));
		map.put("startDate", startDate);
		map.put("endDate", endDate);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/report" })
	public ModelAndView report(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/log/logReport");

		String startDate = StringUtils.isBlank(request
				.getParameter("startDate")) ? TimeUtils.getDate(new Date(), -6)
				: request.getParameter("startDate");
		String endDate = StringUtils.isBlank(request.getParameter("endDate")) ? TimeUtils
				.getDate(new Date(), 0) : request.getParameter("endDate");

		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 0;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<LogReportDTO> list = this.logInfoDao.getReportList(startDate, endDate,
				pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.userInfoDao.getUserCountByRoleId(-1));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("reportDTOList", list);
		map.put("pageBean", pageBean);
		map.put("userInfo", (UserInfo) session.getAttribute("systemUserInfo"));
		map.put("startDate", startDate);
		map.put("endDate", endDate);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}
}